﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using TierService;
using OrderMgr.SysSetting;
using TierApplication;

namespace OrderMgr.CustomerInfo
{
    public partial class frmCustomer : TierService.frmTemplate
    {
        public string _CustID;

        public frmCustomer()
        {
            InitializeComponent();
        }

        private void initForm()
        {
            Utility.DataToLookupComboxEx(txtCustTypeID, C_DMB.GetListByType(C_DMB.DType.CustTypeID.ToString()), "CodeID", "CodeName");
            Utility.DataToLookupComboxEx(txtCustSource, C_DMB.GetListByType(C_DMB.DType.CustSource.ToString()), "CodeID", "CodeName");
        }


        private void btnSearch_Click(object sender, EventArgs e)
        {
            string ls_where = "";

            if ((txtCustName.Text.Trim() != "") && (ckbCustName.Checked))
                ls_where = ls_where + " and CustName  like '%" + txtCustName.Text + "%'";

            if ((txtMobile.Text != "") && (ckbMobile.Checked))
                ls_where = ls_where + " and Mobile like '%" + txtMobile.Text + "%'";

            if ((txtContactAddress.Text != "") && (ckbContactAddress.Checked))
                ls_where = ls_where + " and ContactAddress like '%" + txtContactAddress.Text + "%'";

            if ((txtCustTypeID.Text.Trim() != "") && (ckbCustTypeID.Checked))
                ls_where = ls_where + " and CustTypeID = '" + (string)txtCustTypeID.EditValue + "'";

            if ((txtCustSource.Text.Trim() != "") && (ckbCustSource.Checked))
                ls_where = ls_where + " and CustSource = '" + (string)txtCustSource.EditValue + "'";

            //if (ceFbqj.Checked)
            //{
            //    ls_where = ls_where + " and  DATEDIFF(year,a.Birthday,a.HspDateIn) >= '" + txtFbqjMin.Value + "'";
            //    ls_where = ls_where + " and  DATEDIFF(year,a.Birthday,a.HspDateIn) <= '" + txtFbqjMax.Value + "'";
            //}

            //if ((ceHspDateIn.Checked) && (txtHspDateInMin.Text != "") && (txtHspDateInMax.Text != ""))
            //{
            //    ls_where = ls_where + " and a.HspDateIn>='" + txtHspDateInMin.Text + "' and a.HspDateIn <='" + txtHspDateInMax.Text + "'";
            //}

            ckbAll.Checked = false;
            T_Customer mCustomer = new T_Customer();
            DataSet dsCustomer = mCustomer.GetList(ls_where);
            gcCustomer.DataSource = dsCustomer.Tables[0];
        }

        private void btnExportExcel_Click(object sender, EventArgs e)
        {
            TierService.Utility.DataExportToExcel(gvCustomer, "客户信息");
        }


        private void gvCustomer_DoubleClick(object sender, EventArgs e)
        {
            if (gvCustomer.RowCount < 1) return;
            DataRow dr = gvCustomer.GetDataRow(gvCustomer.FocusedRowHandle);
            string sID = dr["CustID"].ToString();

            if (gFuncType == Utility.FuncType.Select)
            {
                _CustID = sID;
                DialogResult = System.Windows.Forms.DialogResult.OK;
            }
            else
            {
                frmCustomerDetail fDetail = new frmCustomerDetail();
                fDetail.OpMode = TierService.Utility.OperateMode.Query;
                fDetail._CustomerID = sID;
                fDetail.ShowDialog();
            }
        }


        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (gvCustomer.RowCount < 1) return;
            if (MessageBox.Show("是否要删除此客户记录，删除后将无法恢复!", "确认", MessageBoxButtons.OKCancel) == DialogResult.Cancel)
                return;
            DataRow dr = gvCustomer.GetDataRow(gvCustomer.FocusedRowHandle);
            string sID = dr["CustID"].ToString();

            T_Customer mCustomer = new T_Customer();
            mCustomer.Delete(sID);

            btnSearch_Click(null, null);
        }


        private void gvCustomer_CustomDrawRowIndicator(object sender, DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventArgs e)
        {
            e.Appearance.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Far;
            if (e.Info.IsRowIndicator)
            {
                if (e.RowHandle >= 0)
                {
                    e.Info.DisplayText = (e.RowHandle + 1).ToString();
                }
                else if (e.RowHandle < 0 && e.RowHandle > -1000)
                {
                    e.Info.Appearance.BackColor = System.Drawing.Color.AntiqueWhite;
                    e.Info.DisplayText = "G" + e.RowHandle.ToString();
                }
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            frmCustomerDetail fDetail = new frmCustomerDetail();
            fDetail.OpMode = TierService.Utility.OperateMode.Insert;
            fDetail.ShowDialog();

            btnSearch_Click(null, null);
        }

        private void btnModify_Click(object sender, EventArgs e)
        {
            if (gvCustomer.RowCount < 1) return;
            DataRow dr = gvCustomer.GetDataRow(gvCustomer.FocusedRowHandle);
            string sID = dr["CustID"].ToString();

            frmCustomerDetail fDetail = new frmCustomerDetail();
            fDetail.OpMode = TierService.Utility.OperateMode.Update;
            fDetail._CustomerID = sID;
            if (fDetail.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                btnSearch_Click(null, null);
            }
        }

        private void frmPatientInfo_Load(object sender, EventArgs e)
        {
            initForm();

        }

        private void gvCustomer_RowCellClick(object sender, DevExpress.XtraGrid.Views.Grid.RowCellClickEventArgs e)
        {
            if (gvCustomer.RowCount < 1) return;
            DataRow dr = gvCustomer.GetDataRow(gvCustomer.FocusedRowHandle);
            //dr["IsChecked"] = (dr["IsChecked"] == "1") ? "0" : "1";
        }

        private void ceAll_CheckedChanged(object sender, EventArgs e)
        {
            for (int i = 0; i < gvCustomer.DataRowCount; i++)
            {
                DataRow dr = gvCustomer.GetDataRow(i);
                dr["IsChecked"] = ckbAll.Checked ? "1" : "0";
            }
        }


        private int GetSelectRowCount()
        {
            int iCount = 0;
            for (int i = 0; i < gvCustomer.DataRowCount; i++)
            {
                DataRow dr = gvCustomer.GetDataRow(i);
                if (dr["IsChecked"] == "1")
                {
                    iCount = iCount + 1;
                }
            }
            return iCount;
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void btnImport_Click(object sender, EventArgs e)
        {
            //0联系人	1客户来源	 2客户类别	3联系地址 	4联系电话	 5手机号码	6性别	7电子信箱	 8邮编	9备注
            DataSet dsExcel = new DataSet();
            string sFileName = Utility.ShowOpenFileDialog();
            if (sFileName == "") return;
            Cursor current = Cursor.Current;
            Cursor.Current = Cursors.WaitCursor;
            try
            {
                string sSql = "";
                dsExcel = Utility.ReadExcel(sFileName);
            }
            finally
            {
                Cursor.Current = current;
            }
            string sPName = "";
            TierService.WaitDialog.CreateWaitDialog("数据正在导入", "请等待");

            for (int i = 0; i < dsExcel.Tables[0].Rows.Count; i++)
            {
                try
                {
                    DataRow dr = dsExcel.Tables[0].Rows[i];
                    T_Customer mCustomer = new T_Customer();
                    sPName = dr[0].ToString();
                    if (sPName == "")
                    {
                        MessageBox.Show("存在客户姓名为空,不能导入,请查对Excel数据!");
                        TierService.WaitDialog.CloseWaitDialog();
                        return;
                    }

                    mCustomer.CustName = dr[0].ToString();
                    mCustomer.ContactName = dr[0].ToString();
                    string sCustSourceID = C_DMB.GetCodeIDByCodeName(C_DMB.DType.CustSource.ToString(), dr[1].ToString());
                    if (sCustSourceID == "")
                    {
                        MessageBox.Show("客户来源'" + dr[1].ToString() + "'在参照表中不存在,请查对Excel数据!");
                        TierService.WaitDialog.CloseWaitDialog();
                        return;
                    }
                    string sCustTypeID = C_DMB.GetCodeIDByCodeName(C_DMB.DType.CustTypeID.ToString(), dr[2].ToString());
                    if (sCustTypeID == "")
                    {
                        MessageBox.Show("客户类别'" + dr[2].ToString() + "'在参照表中不存在,请查对Excel数据!");
                        TierService.WaitDialog.CloseWaitDialog();
                        return;
                    }
                    mCustomer.CustSource = sCustSourceID;
                    mCustomer.CustTypeID = sCustTypeID;
                    mCustomer.ContactAddress = dr[3].ToString();
                    mCustomer.Phone = dr[4].ToString();
                    mCustomer.Mobile = dr[5].ToString();
                    if (mCustomer.Mobile == "")
                    {
                        MessageBox.Show("客户'" + sPName + "'手机号码不能为空,请查对Excel数据!");
                        TierService.WaitDialog.CloseWaitDialog();
                        return;
                    }
                    string sSex = C_DMB.GetCodeIDByCodeName(C_DMB.DType.Sex.ToString(), dr[6].ToString());
                    mCustomer.Sex = sSex;
                    mCustomer.Email = dr[7].ToString();
                    mCustomer.PostalCode = dr[8].ToString();
                }
                catch (Exception ex)
                {
                    MessageBox.Show("读取客户Excel信息错误,错误原因为：" + ex.Message);
                    TierService.WaitDialog.CloseWaitDialog();
                    return;
                }
            }

            for (int i = 0; i < dsExcel.Tables[0].Rows.Count; i++)
            {
                try
                {
                    DataRow dr = dsExcel.Tables[0].Rows[i];
                    T_Customer mCustomer = new T_Customer();
                    sPName = dr[0].ToString();
                    mCustomer.CustName = dr[0].ToString();
                    mCustomer.ContactName = dr[0].ToString();
                    string sCustSourceID = C_DMB.GetCodeIDByCodeName(C_DMB.DType.CustSource.ToString(), dr[1].ToString());
                    string sCustTypeID = C_DMB.GetCodeIDByCodeName(C_DMB.DType.CustTypeID.ToString(), dr[2].ToString());
                    mCustomer.CustSource = sCustSourceID;
                    mCustomer.CustTypeID = sCustTypeID;
                    mCustomer.ContactAddress = dr[3].ToString();
                    mCustomer.Phone = dr[4].ToString();
                    mCustomer.Mobile = dr[5].ToString();
                    string sSex = C_DMB.GetCodeIDByCodeName(C_DMB.DType.Sex.ToString(), dr[6].ToString());
                    mCustomer.Sex = sSex;
                    mCustomer.Email = dr[7].ToString();
                    mCustomer.PostalCode = dr[8].ToString();

                    string sSql = "select CustID  from T_Customer where Mobile = '" + mCustomer.Mobile + "'";
                    DataSet dsRow = DBUtility.DbHelperSQL.Query(sSql);
                    if (dsRow.Tables[0].Rows.Count == 0)
                    {
                        mCustomer.CustID = Utility.GetSeqval(Utility.SeqName.CustID.ToString());
                        mCustomer.Add();
                    }
                    else
                    {
                        string sCustID = dsRow.Tables[0].Rows[0]["CustID"].ToString();
                        mCustomer.CustID = sCustID;
                        mCustomer.Update();
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("客户：" + sPName + ",数据导入错误,错误原因为：" + ex.Message);
                }

            }
            TierService.WaitDialog.CloseWaitDialog();
            MessageBox.Show("数据导入完毕!");
        }


    }
}